package com.jie.deletefile.FileComent;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/12.
 */
public class MemoryManager {


    /**
     * 设备完整运行内存 单位 B
     */
    public static long getPhoneTotalRamMemory() {
        try {
            FileReader fr = new FileReader("/proc/meminfo");
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            String[] array = text.split("\\s+");
            return Long.valueOf(array[1]) * 1024; // 原为 kb, 转为 b
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 设备空闲运行内存 单位 B
     */
    public static long getPhoneFreeRamMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(info);
        return info.availMem;
    }




    public static String getPhoneInSDCardPath() {
        //先判断SD卡是否是挂载状态，不是则返回null,是就返回绝对狙击那个
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }

        return Environment.getExternalStorageDirectory().getAbsolutePath();

    }

    /**
     * 获取手机外置 sdcard 路径, 为 null 表示无
     */
    public static String getPhoneOutSDCardPath() {

        Map<String, String> map = System.getenv();

        if (map.containsKey("SECOND_STORAGE")) {
            String path = map.get("SECOND_STORAGE");
            String[] paths = path.split(":");

            if (paths == null || paths.length <= 0) {
                return null;
            }
            return paths[0];
        }
        return null;
    }


    /**
     * 设备外置存储 SDCard 全部大小 单位 B , 当没有外置卡时,大小为 0
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static long getPhoneOutSDCardSize(Context context) {

        try {
            File path = new File(getPhoneOutSDCardPath());
            if (path == null) {
                return 0;
            }

            StatFs stat = new StatFs(path.getPath());
            long blockCount = stat.getBlockCountLong();
            long blockSize = stat.getBlockSizeLong();

            return blockCount * blockSize;
        } catch (Exception e) {
            Toast.makeText(context, "外置存储卡异常", Toast.LENGTH_SHORT).show();
            return 0;
        }

    }

    /**
     * 设备外置存储 SDCard 空闲大小 单位 B
     */
    public static long getPhoneOutSDCardFreeSize(Context context) {
        try {
            File path = new File(getPhoneOutSDCardPath());
            if (path == null) {
                return 0;
            }

            StatFs stat = new StatFs(path.getPath());
            long blockCount = stat.getAvailableBlocksLong();
            long blockSize = stat.getBlockSizeLong();

            return blockCount * blockSize;
        } catch (Exception e) {
            Toast.makeText(context, "外置存储卡异常", Toast.LENGTH_SHORT).show();
            return 0;
        }
    }

    /**
     * 设备自身存储全部大小 单位 B
     */
    public static long getPhoneSelfSize() {
        //根目录
        File path = Environment.getRootDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long blockCount = stat.getBlockCountLong();
        long rootFileSize = blockCount * blockSize;
        //下载目录
        path = Environment.getDownloadCacheDirectory();
        stat = new StatFs(path.getPath());
        blockSize = stat.getBlockSizeLong();
        blockCount = stat.getBlockCountLong();
        long cacheFileSize = blockCount * blockSize;
        return rootFileSize + cacheFileSize;
    }

    /**
     * 设备自身存储空闲大小 单位 B
     */
    public static long getPhoneSelfFreeSize() {
        File path = Environment.getRootDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long blockCount = stat.getAvailableBlocksLong();
        long rootFileSize = blockCount * blockSize;
        path = Environment.getDownloadCacheDirectory();
        stat = new StatFs(path.getPath());
        blockSize = stat.getBlockSizeLong();
        blockCount = stat.getAvailableBlocksLong();
        long cacheFileSize = blockCount * blockSize;
        return rootFileSize + cacheFileSize;
    }

    /**
     * 设备内置存储卡全部大小(手机自带 32G 存储空间?) 单位 B
     */
    public static long getPhoneSelfSDCardSize() {
        String sdcardState = Environment.getExternalStorageState();
        if (!sdcardState.equals(Environment.MEDIA_MOUNTED)) {
            return 0;
        }
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long blockCount = stat.getBlockCountLong();
        return blockCount * blockSize;
    }


    /**
     * 设备内置存储卡空间大小(手机自带 32G 存储空间?) 单位 B
     */
    public static long getPhoneSelfSDCardFreeSize() {
        String sdcardState = Environment.getExternalStorageState();
        if (!sdcardState.equals(Environment.MEDIA_MOUNTED)) {
            return 0;
        }
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long availaBlock = stat.getAvailableBlocksLong();
        return availaBlock * blockSize;
    }

    /**
     * 获取手机总存储大小
     */
    public static long getPhoneAllSize() {
        File path = Environment.getRootDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long blockCount = stat.getBlockCountLong();
        long rootFileSize = blockCount * blockSize;
        path = Environment.getDataDirectory();
        stat = new StatFs(path.getPath());
        blockSize = stat.getBlockSizeLong();
        blockCount = stat.getBlockCountLong();
        long dataFileSize = blockCount * blockSize;
        path = Environment.getDownloadCacheDirectory();
        stat = new StatFs(path.getPath());
        blockSize = stat.getBlockSizeLong();
        blockCount = stat.getBlockCountLong();
        long cacheFileSize = blockCount * blockSize;
        return rootFileSize + dataFileSize + cacheFileSize;
    }

    /**
     * 获取手机总闲置存储大小
     */
    public static long getPhoneAllFreeSize() {
        File path = Environment.getRootDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long blockCount = stat.getAvailableBlocksLong();
        long rootFileSize = blockCount * blockSize;
        path = Environment.getDataDirectory();
        stat = new StatFs(path.getPath());
        blockSize = stat.getBlockSizeLong();
        blockCount = stat.getAvailableBlocksLong();
        long dataFileSize = blockCount * blockSize;
        path = Environment.getDownloadCacheDirectory();
        stat = new StatFs(path.getPath());
        blockSize = stat.getBlockSizeLong();
        blockCount = stat.getAvailableBlocksLong();
        long cacheFileSize = blockCount * blockSize;
        return rootFileSize + dataFileSize + cacheFileSize;
    }


}
